<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

$title = $txt['bnt_beacon_title'];
bigtitle($title);

// Retrieve the user and ship information
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Figure out where the player is in the Universe.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$beacon_text = (isset($_POST['beacon_text'])) ? $_POST['beacon_text'] : '';

$allowed_rsw = "N";

if ($playerinfo['dev_beacon'] > 0)
{
	$result = $smcFunc['db_query']('', '
		SELECT allow_beacon
		FROM {raw:db_zones}
		WHERE zone_id = {int:zone_id}',
		array(
			'db_zones' => $dbtables['zones'],
			'zone_id' => $sectorinfo['zone_id']
		)
	);
	$zoneinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	if ($zoneinfo['allow_beacon'] == 'N')
		echo $txt['bnt_beacon_notpermitted'], '<br><br>';
	elseif ($zoneinfo['allow_beacon'] == 'L')
	{
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_zones}
			WHERE zone_id = {int:zone_id}',
			array(
				'db_zones' => $dbtables['zones'],
				'zone_id' => $sectorinfo['zone_id']
			)
		);
	    $zoneowner_info = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		$result = $smcFunc['db_query']('', '
			SELECT team
			FROM {raw:db_ships}
			WHERE ship_id = {int:owner}',
			array(
				'db_ships' => $dbtables['ships'],
				'owner' => $zoneowner_info['owner']
			)
		);
	    $zoneteam = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);

		if ($zoneowner_info['owner'] != $playerinfo['ship_id'])
		{
			if (($zoneteam['team'] != $playerinfo['team']) || ($playerinfo['team'] == 0))
				echo $txt['bnt_beacon_notpermitted'], '<br><br>';
			else
				$allowed_rsw = "Y";
		}
		else
			$allowed_rsw = "Y";
	}
	else
		$allowed_rsw = "Y";

	if ($allowed_rsw == "Y")
	{
		if ($beacon_text == "")
		{
			if ($sectorinfo['beacon'] != "")
				echo $txt['bnt_beacon_reads'], ': "', $sectorinfo['beacon'], '"<br><br>';
			else
				echo $txt['bnt_beacon_none'], '<br><br>';

			echo '	<form action=', $scripturl, '?action=blacknova;sa=beacon method=post>
					<table>
						<tr>
							<td>', $txt['bnt_beacon_enter'], ':</td>
							<td><input type=text name=beacon_text size=40 maxlength=80></td>
						</tr>
					</table>
					<input type=submit value=', $txt['bnt_submit'], '><input type=reset value=', $txt['bnt_reset'], '>
					</form>';
		}
		else
		{
			$beacon_text = trim(strip_tags($beacon_text));
			echo $txt['bnt_beacon_nowreads'], ': "', $beacon_text, '"<br><br>';
			
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_universe}
				SET beacon = {string:beacon_text}
				WHERE sector_id = {int:sector_id}',
				array(
					'db_universe' => $dbtables['universe'],
					'beacon_text' => $beacon_text,
					'sector_id' => $sectorinfo['sector_id']
				)
			);
			
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET dev_beacon = dev_beacon - 1
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'beacon_text' => $beacon_text,
					'ship_id' => $playerinfo['ship_id']
				)
			);
		}
	}
}
else
{
	echo $txt['bnt_beacon_donthave'], '<br><br>';
}

TextMainMenu();

?>
